#coding= UTF-8
try:
    import socket, sys, traceback, threading, time, subprocess
    import wx
    import Frame

    lib = subprocess.Popen("lib.dll", stdin=subprocess.PIPE, stdout=subprocess.PIPE)

    class Game(object):
        def __init__(self):
            self.N = 15
            self.M = 15
            self.colors = ["black", "white"]
            self.current = 0
            self.board = [[0]*self.M for x in range(self.N)]
            self.AI_excute = [False, True]
            self.steps = []

            self.game_over = False

        def go(self, x, y):
            print(x, y)
            # 检查
            if x<0 or y<0 or x>=self.N or y>=self.M:
                # 暂不确定该怎么做
                # raise("not yet!")
                return
            if self.board[x][y]:
                # 有棋子了,啥也不做
                return
            if self.AI_excute[self.current]:
                # 该AI走
                return
            if self.game_over:
                # 已经取胜
                print("game overed")
                return

            # TODO // 调用走子引擎
            self.move(self.current, x, y)

            # 上板
            self.frame.AddAtom(x, y, self.colors[self.current])
            self.board[x][y] = 1 + self.current

            if self.is_win():
                self.frame.win("Human")
                return

            # 换手
            self.current = 1-self.current

            # AI行动
            if self.AI_excute[self.current]:
                ai = threading.Thread(target=self.AI_go)
                ai.setDaemon(True)
                ai.start()

        def AI_go(self):

            if self.game_over:
                # 已经取胜
                print("game overed")
                return

            x, y = self.excute(self.current)

            # TODO // 调用走子引擎
            self.move(self.current, x, y)

            # 上板
            self.frame.AddAtom(x, y, self.colors[self.current])
            self.board[x][y] = 1 + self.current

            # 换手
            self.current = 1-self.current

            if self.is_win():
                self.frame.win("AI")
                return

            # AI行动
            if self.AI_excute[self.current]:
                self.AI_go()

        def move(self, current, x, y):
            lib.stdin.write(("move %d %d %d\n"%(current, x, y)).encode(encoding="utf-8"))
            lib.stdin.flush()

        def excute(self, current):
            lib.stdin.write(("excute %d\n"%current).encode(encoding="utf-8"))
            lib.stdin.flush()
            result = lib.stdout.readline().decode()
            print("result:%s"%result)
            x, y = map(int, result.split(" "))
            return x, y

        def is_win(self):
            lib.stdin.write("isover\n".encode(encoding="utf-8"))
            lib.stdin.flush()
            result = lib.stdout.readline().decode()
            result = int(result)
            if result:
                self.game_over = True
                return True
            return False

    def main():
        game = Game()
        app=wx.App(False)
        game.frame = Frame.start(game)
        app.MainLoop()

    if __name__ == "__main__":
        main()
finally:
    traceback.print_exc()
    print("so what???")
    input()
    
